﻿using Newtonsoft.Json;
using System.Web;
using System.Web.Mvc;

namespace Web
{
    public static class Tool
    {
        public static string SerializeObject(object value)
        {
            string result = JsonConvert.SerializeObject(value);
            return result;
        }

        public static T DeserializeObject<T>(string value)
        {
            var result = JsonConvert.DeserializeObject<T>(value);
            return result;
        }
    }

    /// <summary>
    /// JsonResult扩展
    /// </summary>
    public static class JsonResultExtension
    {
        /// <summary>
        /// 操作结果扩展方法：根据操作是否成功及相关描述转化为JsonResult
        /// </summary>
        public static JsonResult ToJsonResult(bool result, string des)
        {
            var fAnonymousType4 = new
            {
                result = result,
                des = des
            };
            return new JsonResult()
            {
                Data = (object)fAnonymousType4,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    }
}